package com.dmy.ui;

import com.dmy.config.GameConfig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;


/**
 * 用于充当画板的Jpanel类 所有的画面都在这个类中paint方法绘制
 */
public class MyJPanel extends JPanel {

        private List<Tank> tanks; // 敌人坦克
        private List<Tank> myTanks; // 自身坦克
        private JPanel This;

        public MyJPanel(){
            tanks = new LinkedList<Tank>();
            myTanks = new LinkedList<Tank>();
            This = this;
        }
        public void addMyTank(Tank tank){
            myTanks.add(tank);
            this.repaint();
        }
        public void addEnyTank(Tank tank){
            tanks.add(tank);
            this.repaint();
        }
        public int getEnyTankNum(){
            return tanks.size();
        }
        public List<Tank> getEnyTanks(){
            return this.tanks;
        }
        public List<Tank>  getMyTanks(){
            return this.myTanks;
        }


    @Override
    public void paint(Graphics g) {
          super.paint(g);
         g.fillRect(0,0, GameConfig.WINDOW_WIDTH,GameConfig.WINDOW_HEIGHT);
         tanks.forEach(t->t.paint(g));
         myTanks.forEach(mt->mt.paint(g));
        initGameInfo((Graphics2D)g);
    }

    // 绘制地图下方信息
    private void initGameInfo(Graphics2D g) {
         g.setFont(new Font("楷体",Font.BOLD,18));
        g.setColor(Color.MAGENTA);
        g.drawString(String.format("敌军数量: %s",this.getEnyTankNum()),30,GameConfig.WINDOW_HEIGHT+30);

        List<Tank> myTanks = this.getMyTanks();

        g.drawString(String.format("玩家分数: %s 敌人",myTanks.size()>0?((MyTank)myTanks.get(0)).getAttackNum():0),200,GameConfig.WINDOW_HEIGHT+30);

        g.drawString(String.format("生命数: %s",myTanks.size()>0?((MyTank)myTanks.get(0)).getLifeNum():0),400,GameConfig.WINDOW_HEIGHT+30);

        g.drawString("快捷键：‘ + ’添敌人 ;‘ F ’加生命",540,GameConfig.WINDOW_HEIGHT+30);

    }

}
